<template>
  <el-dialog
    title="标签申请"
    :visible.sync="visible"
    width="30%"
    top="13vh"
    :before-close="closeHandle"
    :close-on-click-modal="false">
    <el-form :model="tagApplyForm" :rules="tagApplyRules" ref="tagApplyForm" @keyup.enter.native="tagApplyHandle" label-width="85px">
      <el-form-item label="标签名字" prop="name">
        <el-input v-model="tagApplyForm.name" placeholder="标签名字"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeHandle">取消</el-button>
      <el-button :disabled="buttonLoad" type="primary" @click="tagApplyHandle">申请</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    name: "tag-apply-dialog",
    data() {
      return {
        // 弹出框显示状态[true-显示 false-隐藏]
        visible: false,
        // 标签申请表单对象
        tagApplyForm: {
          // 标签名字
          name: '',
        },
        // 按钮是否可被点击[false-可以被点击 true-不可被点击]
        buttonLoad: false,
        // 标签申请表单校验规则
        tagApplyRules: {
          name: [
            { required: true, message: '标签名字不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init() {
        this.visible = true;
      },
      tagApplyHandle() {
        this.$refs['tagApplyForm'].validate((valid) => {
          if (!valid) {
            return
          }
          // 按钮不可被点击
          this.buttonLoad = true

          this.$axios.$post('/blog/tag/apply/save', this.tagApplyForm)
            .then(() => {
              this.notify('success', '操作成功', `’${this.tagApplyForm.name}‘标签申请成功`)

              // 关闭弹出框
              this.closeHandle()
            })
            .catch(() => {})
            .finally(() => {
              // 按钮恢复可点击
              this.buttonLoad = false;
            })
        })
      },
      // 弹出框关闭处理事件
      closeHandle() {
        // 关闭弹出框
        this.visible = false;
        // 清空系统用户表单信息
        this.$refs['tagApplyForm'].resetFields();

      },
    }
  }
</script>

<style scoped>

</style>
